﻿package game 
{	
	import common.Utility;
	import flash.display.DisplayObject;
	
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.utils.getDefinitionByName;
	
	import gs.TweenMax;
	import gs.easing.Linear;
	import gs.easing.Quart;
	
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class BetPlace extends MovieClip {		
		public var betHit:InteractiveObject;
		public var chipArr:Array;
		private var betValue:Number = 0;
		private var nextY:int = 0;
		private var dis:int = 5;
		private var addBetDuration:Number = 0.3;
		
		private var initChipValueArr:Array;
		private var chipClassMap:Object;
		
		public function BetPlace(betHit:InteractiveObject, initChipValueArr:Array) {		
			this.initChipValueArr = initChipValueArr;
			this.betHit = betHit;
			
			this.addChild(this.betHit);
			
			this.x = this.betHit.x;
			this.y = this.betHit.y;
			this.betHit.x = 0;
			this.betHit.y = 0;
			
			this.chipArr = new Array();
			
			this.chipClassMap = new Object();
			for (var i:int = 0; i < this.initChipValueArr.length; i++) {
				this.chipClassMap["chipValue" + this.initChipValueArr[i]] = "Chip" + i;
			}
		}
		
		/**
		 * @author Daisy
		 * @param	chip
		 */
		public function addBet(chip:Chip):void {		
			var chipNameTmp:String = chip.chipName.substr(0, 4) + Number(chip.chipName.substr(4, 2)) % 6;			
			
			//var ChipClass:Class = getDefinitionByName(chip.chipName) as Class;
			var ChipClass:Class = getDefinitionByName(chipNameTmp) as Class;
			
			var aniChip:MovieClip = MovieClip(new ChipClass());
			aniChip.mouseEnabled = false;
			
			//add to parent so the the animation chip will be over all betplaces
			this.parent.addChild(aniChip);
			aniChip.valueTf.text = chip.value.toString();
			
			var pos:Point = new Point(chip.x, chip.y);
			pos = chip.parent.localToGlobal(pos);
			pos = this.parent.globalToLocal(pos);
			aniChip.x = pos.x;
			aniChip.y = pos.y;
			
			TweenMax.to(aniChip, this.addBetDuration, { x:this.x, y:this.y + this.nextY, onComplete:addBetAnimationCompleteHandler, onCompleteParams:[aniChip]} );
			this.nextY -= this.dis;
			
			this.betValue += chip.value;
			this.betValue = Utility.fixNumber(this.betValue);
			
		}
		
		/**
		 * @author Daisy
		 * @param	aniChip
		 */
		private function addBetAnimationCompleteHandler(aniChip:DisplayObject):void {
			this.parent.removeChild(aniChip);				
			updateChips();
		}
		
		/**
		 * @author Daisy
		 * @return
		 */
		public function removeTopChip():Boolean {
			if (this.chipArr.length) {
				var topChip:MovieClip = this.chipArr.pop();
				this.betValue -= topChip.value;
				this.betValue = Utility.fixNumber(this.betValue);
				removeChild(topChip);
				this.nextY += this.dis;
				return true;
			}
			return false;
		}
		
		public function updateInitChipValue(initChipValueArr:Array):void {
			this.initChipValueArr = initChipValueArr;
			this.chipClassMap = new Object();
			for (var i:int = 0; i < this.initChipValueArr.length; i++) {
				this.chipClassMap["chipValue" + this.initChipValueArr[i]] = "Chip" + i;
			}
		}
		
		/**
		 * @author Daisy
		 */
		public function updateChips():void {
			//trace("initChipValueArr: " + this.initChipValueArr);			
			var initChipValues:Array = Utility.clone(this.initChipValueArr);
			var chipVal:Number = Number(initChipValues.pop());
			var betVal:Number = this.betValue;
			var chipPileValues:Array = new Array();
			var obj:Object;
			while (betVal > 0) {
				if (betVal >= chipVal) {
					obj = new Object();
					obj.chip = chipVal;
					obj.value = chipVal;
					chipPileValues.push(obj);
					//chipPileValues.push(chipVal);
					betVal -= chipVal;
					betVal = Utility.fixNumber(betVal);
				}else {
					if (initChipValues.length == 0)
					{
						obj = new Object();
						obj.chip = chipVal;
						obj.value = betVal;
						chipPileValues.push(obj);
						//chipPileValues.push(betVal);
						break;
					}
					chipVal = Number(initChipValues.pop());
				}
			}
			
			var differentChipInd:int = 0;
			var lookupLength:int = Math.min(chipPileValues.length, this.chipArr.length);
			for (var i:int = 0; i < lookupLength; i++) {
				if (chipPileValues[i].chip != this.chipArr[i].value) {
					differentChipInd = i;
					break;
				}
			}
			while (this.chipArr.length > differentChipInd) {
				removeChild(this.chipArr.pop());
			}
			
			var posY:int = - differentChipInd * this.dis;
			for (i = differentChipInd; i < chipPileValues.length; i++) {
				var ChipClass:Class = getDefinitionByName(this.chipClassMap["chipValue" + chipPileValues[i].chip]) as Class;
				var chip:MovieClip = MovieClip(new ChipClass());
				chip.mouseEnabled = false;
				chip.valueTf.mouseEnabled = false;
				chip["value"] = Number(chipPileValues[i].value);
				chip.valueTf.text = chipPileValues[i].value;
				addChild(chip);
				this.chipArr.push(chip);
				chip.y = posY;
				posY -= dis;
			}
			//update next position of animated chip
			this.nextY = posY;
		}
		
		/**
		 * @author Daisy
		 */
		public function clearBet():void {
			this.betValue = 0;
			updateChips();
		}
		
		/**
		 * @author Daisy
		 * @return
		 */
		public function getBetValue():Number {
			return Utility.fixNumber(this.betValue);
		}
		
		/**
		 * @author Daisy
		 * @param	value
		 */
		public function setBetValue(value:Number):void {
			this.betValue = Utility.fixNumber(value);
			updateChips();
		}
	}
}